#!/bin/bash
set -eo pipefail
shopt -s nullglob

# check jdk
jdk_check() {
  # 检查JDK 1.8是否已安装
  if command -v java >/dev/null 2>&1; then
      echo "JDK 已安装"
      java -version
  else
      echo "JDK 未安装，开始安装..."

      # 检查JDK压缩包是否存在
      if [ -f "/jdk/jdk-8u202-linux-x64.tar.gz" ]; then
          echo "找到JDK压缩包"

          # 创建目标目录
          mkdir -p /opt/jdk

          # 解压缩JDK压缩包到目标目录
          tar -xzf /jdk/jdk-8u202-linux-x64.tar.gz -C /opt/jdk

          # 配置环境变量
          echo "export JAVA_HOME=/opt/jdk" >> ~/.bashrc
          echo "export PATH=\$JAVA_HOME/bin:\$PATH" >> ~/.bashrc

          # 更新当前会话环境变量
          export JAVA_HOME=/opt/jdk
          export PATH=$JAVA_HOME/bin:$PATH

          echo "JDK 安装完成"
          java -version
      else
          echo "未找到JDK压缩包，安装失败"
      fi
  fi
}